﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace yaTDL.Core
{
    public abstract class Service
    {

        protected Timer timer;

        public event EventHandler ActionStarted;
        public event EventHandler ActionCompleted;

        public Service()
        {
            timer = new Timer() { AutoReset = false, Enabled = false };

            timer.Elapsed += new ElapsedEventHandler(backupTimer_Elapsed);
        }

        public void Run()
        {
            OnActionStarted();
            DoAction();
            OnActionCompleted();
        }

        protected void backupTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Run();
        }

        protected void OnActionStarted()
        {
            if (ActionStarted != null)
            {
                ActionStarted(this, EventArgs.Empty);
            }
        }

        protected void OnActionCompleted()
        {
            if (ActionCompleted != null)
            {
                ActionCompleted(this, EventArgs.Empty);
            }
        }
        

        /// <summary>
        /// Executes the Service's task.
        /// </summary>
        protected abstract void DoAction();    
    
    }
}
